<template>
  <div class="bankCardInfo">
    <van-nav-bar
        left-arrow
        @click-left="handleClickLeft"
        :title="$t('添加收款银行卡')"
    >
      <template #left>
        <van-icon name="arrow-left" class="nav_left_icon" size="18px"/>
      </template>
    </van-nav-bar>
    <div class="wrapper">
      <div class="card_box">
        <div class=" card_body ">

          <div class="cell_content_list" style="margin-top: 15px">
            <div class="card_title">
              <span class="text_highlight">Select payment method</span>
            </div>
            <div style="display: grid;grid-template-columns: 1fr 1fr;grid-gap: 15px;padding: 15px 0">

              <div class="method_bank" @click="payTypeValue = 'GCash'"
                   :class="{'bank_active':payTypeValue ===  'GCash'}">
                <van-checkbox :model-value="true" checked-color="#3139D3" class="fixed_check"
                              v-show="payTypeValue ===  'GCash'"></van-checkbox>
                <img class="bank_GCash_logo" src="../../assets/my/bank_GCash_logo.png">
              </div>
              <div class="method_bank" @click="payTypeValue = 'PayMaya'"  :class="{'bank_active':payTypeValue ===  'PayMaya'}">
                <van-checkbox :model-value="true" checked-color="#3139D3" class="fixed_check"
                              v-show="payTypeValue ===  'PayMaya'"></van-checkbox>
                <img class="bank_PayMaya_logo" src="../../assets/my/bank_PayMaya_logo.png">
              </div>
              <div class="method_bank" @click="payTypeValue = 'GrabPay'" :class="{'bank_active':payTypeValue ===  'GrabPay'}">
                <van-checkbox :model-value="true" checked-color="#3139D3" class="fixed_check"
                              v-show="payTypeValue ===  'GrabPay'"></van-checkbox>
                <img class="bank_GrabPay_logo" src="../../assets/my/bank_GrabPay_logo.png">
              </div>
              <div class="method_bank" @click="payTypeValue = 'Bank'" :class="{'bank_active':payTypeValue ===  'Bank'}">
                <van-checkbox :model-value="true" checked-color="#3139D3" class="fixed_check"
                              v-show="payTypeValue ===  'Bank'"></van-checkbox>
                <div style="text-align: center">
                  <img class="bank_logo" src="../../assets/my/bank_logo.png">
                  <div style="font-size: 18px;font-weight: 600">Bank</div>
                </div>
              </div>
            </div>
            <div class="card_title">
              <span class="text_highlight">Enter the payment account number</span>
            </div>
            <!--        收款银行-->
            <div class="cell_content_item" v-if="payTypeValue === 'Bank'">
              <div style="position: relative" @click="selectBankClick()">
                <input class="input" v-model="cardInfo.cardIssuingBank" :readonly="true"
                       :placeholder="$t('请选择收款银行')"/>
                <img class="input_icon" src="../../assets/my/input_right.png">
              </div>
            </div>
            <!--        银行账户-->
            <div class="cell_content_item">
              <input class="input" v-model="cardInfo.bankcard" type="tel"
                     oninput="value = value.replace(/[^\d]/g, '');"
                     :placeholder="$t('请输入银行账号')"/>
            </div>
          </div>
        </div>
      </div>
      <div style="text-align: center;padding: 30px ">
        <van-button class="next_confirm" round type="primary"
                    @click="next">{{ $t('下一步') }}
        </van-button>
      </div>
    </div>


    <van-dialog v-model:show="confirmInfoShow" show-cancel-button @confirm="next">
      <template #title>
        <h4 style="margin: 10px">{{ $t('提示') }}</h4>
        <div style="padding:0 15px;font-size:13px;color:#F1562D">
          {{ $t('请确认信息无误后再点击确认按钮') }}
        </div>
      </template>
      <template #default>
        <div class="confirm_form">
          <div class="confirm_item">
            <div class="label">{{ $t('银行名称') }}</div>
            <div class="text">{{ cardIssuingBank }}</div>
          </div>
          <div class="confirm_item">
            <div class="label">{{ $t('银行账号') }}</div>
            <div class="text">{{ bankcard }}</div>
          </div>
        </div>
      </template>
    </van-dialog>

    <van-popup v-model:show="dialogRetentionShow">
      <template #default>
        <div class="dialog_body">
          <div class="content_text">{{ $t('申请马上成功') }}
            <p class="active">{{ $t('只需两步即可立即获得资金') }}</p>
            {{ $t('你真的想退出吗?') }}
          </div>
          <div style="margin-top: 20px;text-align: center">
            <van-button type="primary" round style="width: 80%" @click="dialogRetentionShow = false" >{{
                $t('继续申请')
              }}
            </van-button>
            <van-button class="reject_btn" round @click="$router.replace('/hScmlvxSUkDQ/UjRzDSClllpS')" >
              {{ $t('残忍拒绝') }}
            </van-button>
          </div>
        </div>
      </template>
    </van-popup>
    <!--  选择银行卡  -->
    <van-action-sheet v-model:show="selectBankShow">
      <van-picker @confirm="selectBankChange" @cancel="selectBankShow = false"
                  :columns-field-names="{  text: 'value'}"
                  :columns="selectBankList"/>
    </van-action-sheet>
  </div>
</template>

<script>
import {onMounted, reactive, ref, toRefs} from 'vue'
import {fetchBankCard, selectCitys, addBankCardMultiple, fetchMyCard} from '@/api/home'
import {useI18n} from 'vue-i18n'
import {useRoute} from 'vue-router'
import {useStore} from 'vuex'
import {Dialog, Toast} from "vant";
import router from "@/router";
import {debounce, processNext, setResourcesShow, upLoadAdjustPoint} from '@/utils/tool'
import {upLoadPoint} from "@/api/my";

export default {
  components: {
    [Dialog.Component.name]: Dialog.Component,
  },
  setup() {
    const {t} = useI18n()
    const store = useStore()
    const confirmInfoShow = ref(false)
    const dialogRetentionShow = ref(false)
    const resourcesShow = ref({})
    const payTypeValue = ref('GCash')
    const bankAllList = ref([])
    let cardInfo = reactive({
      isSelected: 1,
      cardIssuingBank: '',
      bankcard: '',
      cardType: 1,
    })
    const payTypeShow = ref(false)
    // 选择收款银行
    const selectBankSheet = reactive({
      selectBankShow: false,
      selectBankText: '',
      selectBankList: [],
      selectBankChange: (value) => {
        cardInfo.cardIssuingBank = value
        selectBankSheet.selectBankShow = false
      },
      selectBankClick: async () => {
        selectBankSheet.selectBankList = bankAllList.value.filter(v => v.bankType !== 'e-wallet').map(v => v.bankName)
        selectBankSheet.selectBankShow = true
      }
    })

    const next = async () => {
      if (payTypeValue.value !== 'Bank') {
        cardInfo.cardIssuingBank = payTypeValue.value
      }
      cardInfo.cardType = payTypeValue.value !== 'Bank' ? 1 : 2
      if (!cardInfo.cardIssuingBank) return Toast(t('请选择收款银行'))
      if (!cardInfo.bankcard) return Toast(t('请输入银行账号'))
      if (cardInfo.cardType === 1 && cardInfo.bankcard.length !== 11) return Toast(t('钱包卡号错误'))
      // 过滤没填写完银行卡
      const params = [cardInfo]
      Toast.loading({
        message: t('加载中'),
        duration: 0
      })
      const res = await addBankCardMultiple(params)
      if (res.code === 1) {
        Toast({
          message: t('提交成功')
        })
        processNext('bankCardInfo')
        upLoadAdjustPoint({eventName: 'ee6ukt'})
      }
    }

    const getSelect = async () => {
      // 获取银行卡列表
      const bankCardRes = await fetchBankCard()
      if (bankCardRes.code === 1) {
        bankAllList.value = bankCardRes.data
      }
      // 清除loading
      store.dispatch('changeLoading', false)
    }
    // 流程显示隐藏
    const getResourcesShow = async () => {
      resourcesShow.value = await setResourcesShow(6)
    }

    function handleClickLeft() {
      upLoadPoint({type: 10})
      dialogRetentionShow.value = true
    }

    onMounted(() => {
      getSelect()
      getResourcesShow()
    })
    return {
      ...toRefs(cardInfo),
      ...toRefs(selectBankSheet),
      bankAllList,
      payTypeValue,
      handleClickLeft,
      payTypeShow,
      dialogRetentionShow,
      confirmInfoShow,
      resourcesShow,
      confirm,
      next,
      cardInfo,
    }
  }
}
</script>

<style lang="less" scoped>
.bankCardInfo {
  overflow: auto;

  .bank_active {
    border: 2px solid #3139D3;
  }

  .title_round {
    height: 30px;
    width: 30px;

  }


  .method_bank {
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #FFFFFF;
    padding: 10px 0;
    border-radius: 10px;
    position: relative;

    .fixed_check {
      position: absolute;
      right: -1px;
      top: -1px;
    }

    .bank_GCash_logo {
      width: 122px;
      height: 30px;
    }

    .bank_PayMaya_logo {
      width: 140px;
      height: 50px;
    }

    .bank_GrabPay_logo {
      width: 108px;
      height: 76px;
    }

    .bank_logo {
      width: 50px;
      height: 50px;
    }
  }

  .add_card_btn {
    text-align: center;
    margin-top: 20px;
    color: #09B592;
    font-size: 14px;
  }

  .wrapper {
  }

  .dialog {
    position: fixed;
    left: 0;
    top: 0;
    background: rgba(0, 0, 0, 0.5);
    width: 100%;
    height: 100%;
    padding-top: 20vh;
    z-index: 999;

    .dialog_body {
      width: 90vw;
      margin: 0 auto;
      background-color: #ffffff;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
      border-radius: 3px;
      max-height: 500px;
      overflow: auto;
      padding: 0 10px;
      box-sizing: border-box;

      .list_item {
        box-sizing: border-box;
        padding: 10px;
        border-bottom: solid 1px #EEEEEE;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .image {
          height: 40px;
          width: 40px;
        }

        .text {
          color: #686868;
          font-size: 14px;
          margin-left: 10px;
          text-align: left;
          flex: 1;
        }

        .active {
          padding: 0 10px;

          img {
            height: 10px;
            width: 13px;
          }
        }
      }
    }
  }

  .card_box {
    box-sizing: border-box;
    border-radius: 10px;
    margin-bottom: 15px;

    .card_body {
      width: 100%;
      border-radius: 10px;
      padding: 10px 0;

      .card_title {
        font-size: 16px;
        margin-bottom: 10px;
        line-height: 20px;

        .text_highlight {
          position: relative;
          color: #15B3B2;
          z-index: 2;
          vertical-align: middle;
          margin-left: 5px;
        }
      }

      .select_pay_box {
        box-sizing: border-box;
        margin: 10px;
        border-radius: 8px;
        background-color: #F6F6F6;

        .pay_item {
          box-sizing: border-box;
          padding: 15px 10px;
          display: flex;
          align-items: center;
          justify-content: space-between;

          .image {
            height: 36px;
            width: 36px;
          }

          .text {
            color: #686868;
            font-size: 18px;
            margin-left: 10px;
            text-align: left;
            flex: 1;
          }
        }
      }
    }
  }

  .confirm_form {
    padding: 15px 15px 0 15px;

    .confirm_item {
      margin-bottom: 15px;

      .label {
        margin-bottom: 10px;
        color: #4a4a4a;
      }

      .text {
        background: #F0EFF5;
        border-radius: 5px;
        padding: 10px;
        border-bottom: 1px solid #D7D5D5;
      }
    }
  }
}
</style>
